<?php
class Leave extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->model("leave_model");
	}

	public function leaveView()
	{
		//View used to Show Leave Data By Months
		$data = array();
		if(!isset($_SESSION['user_state']))
		{
			echo "<script>location.href='/'</script>";
		}
		else
		{
			$data['leave_data'] = $this->leave_model->getLeaveByMonthState(date("Y-m"));
			$this->load->view("/leave/index",$data);
		}
	}

	public function sickLeaveView()
	{
		//View used to Show Sick Leave Data By Months
		$data = array();
		if(!isset($_SESSION['user_state']))
		{
			echo "<script>location.href='/'</script>";
		}
		else
		{
			$data['leave_data'] = $this->leave_model->getSickLeaveByMonthState(date("Y-m"));
			$this->load->view("/sick_leave/index",$data);
		}
	}


	public function detailView()
	{
		$data = array();
		if(!isset($_SESSION['user_state']))
		{
			echo "<script>location.href='/'</script>";
		}
		else
		{
			$id = $this->uri->segment(3);
			$data= array();
			$data['content'] = $this->leave_model->detailState($id);
			$data['id'] = $id;
			$this->load->view("leave/detail",$data);
		}
	}


	public function sickDetailView()
	{
		$data = array();
		if(!isset($_SESSION['user_state']))
		{
			echo "<script>location.href='/'</script>";
		}
		else
		{
			$id = $this->uri->segment(3);
			$data= array();
			$data['content'] = $this->leave_model->detailState($id);
			$data['id'] = $id;
			$this->load->view("sick_leave/detail",$data);
		}
	}
	public function editAction()
	{
		/*Edit Leave Information */
		if(!isset($_SESSION['user_state']))
		{
			echo json_encode(array("code"=>"101"));
		}
		else
		{
			$data = $this->input->post();
			echo json_encode($this->leave_model->editState($data));
		}
	}

	public function pendAction()
	{
		if(!isset($_SESSION['user_state']))
		{
			echo json_encode(array("code"=>"101"));
		}
		else
		{
			$data = $this->input->post();
			echo json_encode($this->leave_model->pendState($data));
		}
	}
	public function getLeaveByMonthAction()
	{
		//According to the post data to return responding leave data
		if(!isset($_SESSION['user_state']))
		{
			echo json_encode(array());
		}
		else
		{
			$data = $this->input->post();
			$date = date("Y-m",strtotime($data['date']));
			echo json_encode($this->leave_model->getLeaveByMonthState($date));
		}
	}

	public function getSickLeaveByMonthAction()
	{
		//According to the post data to return responding sick leave data
		if(!isset($_SESSION['user_state']))
		{
			echo json_encode(array());
		}
		else
		{
			$data = $this->input->post();
			$date = date("Y-m",strtotime($data['date']));
			echo json_encode($this->leave_model->getSickLeaveByMonthState($date));
		}
	}

	public function newLeaveView()
	{
		//View used to apply a new Leave Apply Page
		$data = array();
		if(!isset($_SESSION['user_state']))
		{
			echo "<script>location.href='/'</script>";
		}
		else
		{
			
			$this->load->view("/leave/new_leave",$data);
		}
	}


	public function newSickLeaveView()
	{
		$data = array();
		if(!isset($_SESSION['user_state']))
		{
			echo "<script>location.href='/'</script>";
		}
		else
		{
			$this->load->view("/sick_leave/new_leave",$data);
		}
	}
	public function newLeaveAction()
	{
		$data = $this->input->post();
		if(!isset($_SESSION['user_state']))
		{
			echo json_encode(array("code"=>"101"));
		}
		else
		{
			echo json_encode($this->leave_model->insertLeaveApply($data));
		}
	}

	public function cancelLeaveAction()
	{
		$data = $this->input->post();
		if(!isset($_SESSION['user_state']))
		{
			echo json_encode(array("code"=>"101"));
		}
		else
		{
			echo json_encode($this->leave_model->updateApplyCancel($data));
		}
		
	}

	public function assureSickAction()
	{
		$data = $this->input->post();
		if(!isset($_SESSION['user_state']))
		{
			echo json_encode(array("code"=>"101"));
		}
		else
		{
			echo json_encode($this->leave_model->updateApplyAssured($data));
		}
		
	}

	
}